<?php
	include "./application/core/session.php";
	#LIBRARY
		include DIR_LIBRARY 	. "pageMiscLib.php";
		include DIR_LIBRARY 	. "paginationLib.php";
		include DIR_LIBRARY 	. "EmailLib.php";
	#DATABASE
		include DIR_MODEL		. "loginMod.php";
		include DIR_MODEL		. "globalMod.php";
		include DIR_MODEL		. "departmentSubjectCourseMod.php";
		include DIR_MODEL		. "studentInformationMod.php";
		include DIR_MODEL		. "personalInformationMod.php";
		
	#CONTROLLER
		include DIR_CONTROLLER	. "loginCon.php";
		include DIR_CONTROLLER	. "globalCon.php";
		include DIR_CONTROLLER	. "departmentSubjectCourseCon.php";
		include DIR_CONTROLLER	. "studentInformationCon.php";
		include DIR_CONTROLLER	. "personalInformationCon.php";
	
	$PI 			= new WS_PersonalInformationCon();
	$globalCon 		= new WS_GlobalCon();
	$pageLib 		= new PageMisc();
	$PAGINATION 	= new WS_Pagination();
	$DSC 			= new WS_DepartmentSubjectCourseCon();
	$EMAIL			= new WS_Email();
	$STUD 			= new WS_StudentInformationCon();
	
	if ( ((!empty($_SESSION['WB_Login'])) && ($_SESSION['WB_Login'] == 'Y')) || (!empty($_COOKIE['WB_Cookie'])) ) {
		
		$logUserID 		= $globalCon->globalLogID();
	
		$getActiveYear 	= $globalCon->getActiveYear();
		$activeYear 	= $getActiveYear['year_idPK'];
		$yearFrom 		= $getActiveYear['year_from'];
		$yearTo 		= $getActiveYear['year_to'];
		$yearSummer 	= $getActiveYear['is_summer'];
		if ($yearSummer == 'Y') {	$__SYR__ = 'Summer :: SY ' . $yearFrom; }
			elseif ($yearFrom == $yearTo) {	$__SYR__ = '1st Semester :: SY ' . $yearFrom; }
				else { $__SYR__ = '2nd Semester :: SY ' . $yearFrom .'-'.$yearTo; }

		$pagePopup		= $pageLib->pagePopup(DIR_IMAGES);
		$pageFooter		= $pageLib->pageFooter();
		
		if ( ($_SESSION['WB_AccType'] == 'S') || ($_COOKIE['WB_Account'] == 'S') ) {
			
			$strStudPictureName = $globalCon->getStudentPictureName($logUserID);
			$strPicture 	= $strStudPictureName['picture'];
			$strName		= $strStudPictureName['name'];
			$strAccNo		= $strStudPictureName['stud_no'];
			$strCourseID	= $strStudPictureName['course_idPK'];
			$strCourseCode	= $strStudPictureName['course_code'];
			$strCourseDec	= $strStudPictureName['course_desc'];
			$strDeptID		= $strStudPictureName['dept_idFK'];
			
			
			$initSemActive	= $STUD->getSemesterEnrollmentActive($logUserID);
			$initSem		= $DSC->getSemesterLabelValidate($initSemActive['sem_linker_idFK'], $logUserID);
			$_ISREGULAR_	= $initSemActive['is_regular'];
			$_ACTIVESEM_	= $initSemActive['sem_linker_idFK'];
			
			$strStudentSection = $globalCon->getStudentSection($logUserID, $initSemActive['sem_linker_idFK'], $activeYear);
			$strSectionID	= $strStudentSection['section_idPK'];
			$strSectionDec	= $strStudentSection['section_desc'];
			
			$gradeList 		= $STUD->getPassFailedEnrollmentSubject($logUserID, $initSemActive['sem_linker_idFK']);
			if (!empty($gradeList)) {
				$countGradeVal = 0;
				foreach($gradeList as $gradeListValue) {
					if (empty($gradeListValue['grade'])) {
						$countGradeVal++;
					}
				}
			}
			if ( $countGradeVal == 0 ) {
				if ( $initSem['count'] > 0 ) {
					$butEnrollNow = true;
				}
			}
			
			$initActiveValidate = $STUD->getStudSemValidation($logUserID, $strCourseID, $initSemActive['sem_linker_idFK'], $activeYear);
			if ( $initActiveValidate > 0 ) {
				$fontStrike = 'font-strike font-grey';
			}

			$strArrDetails = array('name'		=> $strName,
								   'stud_no'	=> $strAccNo,
								   'course'		=> $strCourseDec,
								   'section'	=> $strSectionDec,
								   'regular'	=> ($initSemActive['is_regular'] == 'Y') ? '' : 'Irregular Student');
								   
			$data = array('cid' => $strCourseID, 'sid' => $strSectionID, 'lnk' => 'ry', 'hash' => hashRandom($strName.$strCourseID.$strSectionDec));	
			
			$strHASHURI			= hashRandom($strName.$strAccNo.$strCourseID.$strCourseCode.$strCourseDec.$strSectionID.$strSectionDec);
			$studSemester 		= $DSC->getStudentSemesteryearCourse($logUserID, $strCourseID);
			$studActiveSemester = $pageLib->studentActiveSemester(DIR_IMAGES, $studSemester, $getVar, $strHASHURI, $butEnrollNow, $fontStrike, $initSemActive['sem_linker_idFK']);
			$activeSemester 	= $STUD->getSemesterEnrollmentActive($logUserID);
			$studentDesignName 	= $pageLib->studentName(DIR_IMAGES, DIR_UPLOAD, $strPicture, $strArrDetails, $data);
			$pageHeader			= $pageLib->pageTopHeader(DIR_UPLOAD, DIR_IMAGES, $strName, $strPicture, 'S', $__SYR__);
			
		}
		else {
			$strPictureName = $globalCon->getPictureName($logUserID);
			$strPicture 	= $strPictureName['picture'];
			$strName		= $strPictureName['name'];
			$strAccNo		= $strPictureName['account_no'];
			$strAccType		= $strPictureName['account_type'];
			$strAccDept		= $strPictureName['dept_idFK'];
			$strAccCourse	= $strPictureName['dept_desc'];
			$pageHeader		= $pageLib->pageTopHeader(DIR_UPLOAD, DIR_IMAGES, $strName, $strPicture, $strAccType, $__SYR__);
		}
		
		$getVar = ( empty($_GET['page']) ) ? 'Home' : $_GET['page'];
		
		if ( $getVar == 'Logout' ) {
			include "./application/mvc/scripts/Logout.php";
		}
		else {
			
			$_ACCTYPE = (!empty($_SESSION['WB_AccType'])) ? $_SESSION['WB_AccType'] :  $_COOKIE['WB_Account'];
			
			$pageAccess = $globalCon->getPageAccess($_ACCTYPE, $getVar);
			
			if ( !empty($pageAccess) ) {
				include "./application/mvc/scripts/" . $pageAccess['directory'] . "/" . $pageAccess['php_file'] . ".php";
			}
			else {
				include "./application/mvc/scripts/Error404.php";
			}
		}
		
	}
	else {
		$TEMP->set_file(array('handle' => DIR_VIEW . 'login.html'));
		include "./application/mvc/scripts/Login.php";
	}
	$TEMP->set_var(array('log_name' 		=> $strName,
						 'log_account_no' 	=> $strAccNo,
						 'dir_css' 			=> DIR_CSS,
						 'dir_js' 			=> DIR_JS,
						 'dir_image' 		=> DIR_IMAGES,
						 'dir_upload' 		=> DIR_UPLOAD,
						 'page_popup'		=> $pagePopup,
						 'page_header'		=> $pageHeader,
						 'page_footer'		=> $pageFooter,
						 'syear'			=> $__SYR__,
						 'year_id'			=> $activeYear,
						 ));
	
	$TEMP->printParse('handle');
?>